package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.dlwx.parttimetalent.R;
import com.google.android.flexbox.FlexboxLayout;

/**
 * Created by Administrator on 2017/11/18.
 */

public class FlowLayout extends FlexboxLayout {
    private Context context;
    private int column;
//
    private int width;
    private int height;

    public FlowLayout(Context context) {
        this(context,null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        TypedArray typedArray=context.getTheme().obtainStyledAttributes(attrs, R.styleable.FlowLayout,defStyleAttr,0);
        int size=typedArray.getIndexCount();
        for(int i=0;i<size;i++){
            int attr=typedArray.getIndex(i);
            switch (attr){
                case R.styleable.FlowLayout_ln_column:
                    column= typedArray.getInteger(attr,0);
                    break;
            }
        }
        typedArray.recycle();
        addChildView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);
        int heightSize=MeasureSpec.getSize(heightMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        if(widthMode==MeasureSpec.EXACTLY){
            width=widthSize;
        }else{

        }
        if(heightMode==MeasureSpec.EXACTLY){
            height=heightSize;
        }
    }
    private void addChildView() {
        if(column!=0){
            for(int i=0;i<column;i++){
                CheckBox checkBox=new CheckBox(context);
                checkBox.setText("shuzhi:"+i);
                checkBox.setGravity(Gravity.CENTER);
                checkBox.setButtonDrawable(null);
                checkBox.setBackgroundResource(R.drawable.selector_tv_blue_bg);
                FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setFlexGrow(1);
                params.setMargins(2,0,0,0);
                checkBox.setLayoutParams(params);
                this.addView(checkBox,i,params);
            }
        }
    }
}
